//
//  MarkedTypeViewController.m
//  FleaFinder
//
//  Created by Hieu Le on 4/7/13.
//  Copyright (c) 2013 Kiwiweb. All rights reserved.
//

#import "MarkedTypeViewController.h"
#import "XmlParserCategoryList.h"
#import "MarkedSummaryListViewController.h"

@interface MarkedTypeViewController ()

@end

@implementation MarkedTypeViewController

//@synthesize selectedMarkedType;

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self.navigationController.navigationBar setTintColor:[UIColor blackColor]];

    [self.navigationController setNavigationBarHidden:NO animated:YES];
}

-(void)viewWillAppear:(BOOL)animated
{
    if(!appDelegate){
        appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    }
    
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    
    CGRect rect = [[UIScreen mainScreen] bounds];
    if(rect.size.height < 500){
        self.view.frame = CGRectMake(0.0, 50.0, rect.size.width, 50.0);
    }
}

-(void) loadXmlDataForMarketType: (MarketType*) markedType fromUrl: (NSString*) urlString// reload:(BOOL) toReload
{
    if(!appDelegate){
        appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    }
    
    //if(toReload){
    if([appDelegate.categoryList count] == 0){
        XmlParserCategoryList *xmlParser = [[XmlParserCategoryList alloc] init];
        [xmlParser parseXML: urlString];
        //currentData = [xmlParser getParsedObjects];
        appDelegate.categoryList = [xmlParser getParsedObjects];
    //}
    }
    //selectedMarkedType = markedType;
    [self.navigationItem setTitle: markedType.name];
    //oldTitle = forMarkedType;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [appDelegate.categoryList count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell == nil){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    // Configure the cell...
    cell.textLabel.text = ((Category*)[appDelegate.categoryList objectAtIndex:indexPath.row]).title;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    UIFont *myFont = [ UIFont fontWithName: @"Georgia" size: 17.0 ];
    cell.textLabel.font  = myFont;
    [cell.textLabel setTextColor:[UIColor colorWithRed:0.188 green:0.188 blue:0.219 alpha:1.0]];

    return cell;
}


#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // NSString *linkMarkedListSummary;

    if(!appDelegate.marketSummaryListViewController){
        
        if(!appDelegate.storyBoard)
            appDelegate.storyBoard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
        
        appDelegate.marketSummaryListViewController = [appDelegate.storyBoard instantiateViewControllerWithIdentifier:@"SBMarkedSummaryList"];
    }
   
    appDelegate.marketSummaryListViewController.isNotTopMarkderMode = YES;
    [appDelegate.marketSummaryListViewController setCategoryList:appDelegate.categoryList];
    
    appDelegate.currentSelectedCategory = (Category*)[appDelegate.categoryList objectAtIndex:indexPath.row];
    
    [appDelegate.marketSummaryListViewController loadXmlData:appDelegate.common.linkMarkedListSummary forMarketType:appDelegate.currentSelectedMarketType andCategory:appDelegate.currentSelectedCategory];
    
    [self.navigationController pushViewController:appDelegate.marketSummaryListViewController animated:YES];
}


@end
